<?php
namespace App\Http\Controllers;

use App\Models\ActPrize;
use App\Models\ActPrizeSetting;
use Illuminate\Http\Request;
use App\Models\Act;
use App\Http\Requests\ActPrizeRequest;

class ActPrizeController extends CommonController
{
    /**
     *获取列表
     */
    public function getIndex()
    {
        $lists = ActPrize::with('act', 'setting')->orderBy('created_at', 'desc')->paginate(10);

//        dd($lists);

        return view('actprize.index')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search()
    {
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        $acts = Act::get();

        return view('actprize.create')->with('acts', $acts);
    }

    /**
     * 执行一个创建
     */
    public function postCreate(ActPrizeRequest $request)
    {
        $data = $request->input();
        $num = $data['num'];
        unset($data['formType'], $data['_token'], $data['num']);
        $result = ActPrize::create($data);
        ActPrizeSetting::create(['act_id' => $data['act_id'], 'prize_id' => $result->id, 'num' => $num]);

        //生成这个奖的二维码
//        $url = '/game/web?id='.$result->id;
//        $file_name = unique_id() . '.png';
//        $qrCode = img_create($url, $file_name,$this->gameDir);

//        ActPrize::where('id', $result->id)->update(['qrcode_detail'=>$qrCode]);

        return AjaxCallbackMessage('奖品类型创建成功', true, url('actprize/index'));
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $acts = Act::get();
        $list = ActPrize::with('act', 'setting')->where('id', $data['id'])->first();

//        dd($list);
        return view('actprize.edit')->with('list', $list)->with('acts', $acts);
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(ActPrizeRequest $request)
    {
        $data = $request->input();
        $id = $data['id'];
        $prize = ActPrize::where('id', $id)->first();
        $lists = ActPrize::get()->toArray();
        if(in_array($data['sort'], array_column($lists,'sort')) && $prize->sort!=$data['sort']){
            return AjaxCallbackMessage('sort不能和别的奖项的sort重复',false);
        }

        $num = $data['num'];
        unset($data['formType'], $data['_token'],$data['num']);
        ActPrize::where('id', $id)->update($data);

        $setting = ActPrizeSetting::where('prize_id', $data['id'])->get();
        if(count($setting)){
            ActPrizeSetting::where('prize_id', $data['id'])->update(['num' => $num,'act_id'=>$data['act_id']]);
        }else{
            ActPrizeSetting::create(['prize_id'=>$data['id'], 'num'=>$num, 'act_id'=>$data['act_id']]);
        }

//        if(!$prize->qrcode_detail){
//            //生成这个奖的二维码
//            $url = '/game/web?id='.$id;
//            $file_name = unique_id() . '.png';
//            $qrCode = img_create($url, $file_name, $this->gameDir);
//
//            ActPrize::where('id', $id)->update(['qrcode_detail'=>$qrCode]);
//        }

        return AjaxCallbackMessage('奖品类型编辑成功', true, url('actprize/index'));
    }

    /**
     * 执行一个删除
     */
    public function postDelete()
    {
    }
}